﻿using System.Collections.Generic;
using System.Text.RegularExpressions;
using BearUtil.Hidden;
using Sirenix.OdinInspector;
using UnityEngine;

namespace BearUtil
{
    public enum TalkPosition 
    {
        Left = 1,
        Right = 2
    }

    public enum BubbleType
    {
        Default = 1,
        Thinking = 2 
    }

    public enum SceneType
    {
        Default = 1,
        Thinking = 2
    }
    
    [CreateNodeMenu(menuName:"自定义节点/台词节点"), Title("台词节点"),NodeWidth(280)]
    public class TalkNode : BearNode
    {
        public override int type
        {
            get => (int) NodeType.TALK;
        }

        [SerializeField]
        [HideLabel, AssetSelector(DropdownWidth = 200, Paths = "Assets/StoryEditor/Nodes/Actor/Data/"), OnValueChanged("actorChanged")]
        public ActorData actor;
        
        [Sirenix.OdinInspector.ReadOnly]
        [LabelText("角色"), PreviewField(55, ObjectFieldAlignment.Center), ShowIf("actor")]
        public Texture header;
    
        /// <summary>
        /// 立绘位置
        /// </summary>
        [LabelText("立绘位置"), ShowIf("actor")]
        public TalkPosition portrait_direction;
        [LabelText("立绘图标"), ValueDropdown("GetPortraitIcon", DropdownWidth = 300), ShowIf("actor")]
        public string icon_portrait;

        [LabelText("展示图片"), ]
        public string[] show_pic;
        
        /// <summary>
        /// 角色首选名称
        /// </summary>
        [LabelText("角色首选名称"), ValueDropdown("GetRoleName", DropdownWidth = 300), ShowIf("actor")]
        public string role_name;
                
        [LabelText("对话气泡样式")]
        public BubbleType lines_bubble = BubbleType.Default;

        [LabelText("场景气泡样式")] 
        public SceneType scene_bubble = SceneType.Default;

        [LabelText("字体速度")]
        public float FontSpeed = 30;
        
        [LabelText("音频"), ValueDropdown("getAudioClip", DropdownWidth = 300)]
        public string VoicClip;
        
        [HideLabel]
        // public AudioPlayData audioData = new AudioPlayData();

        private List<string> getAudioClip()
        {
            var clips = AudioCtrl.Instance.Clips;
            List<string> specialClips = new List<string>();
            for (var i = 0; i < clips.Count; i++)
            {
                if (string.IsNullOrEmpty(actor?.voice_type) || !Regex.Match(@$"{clips[i].name}", actor?.voice_type).Value.Equals(""))
                    specialClips.Add(clips[i].name);
            }
            
            return specialClips;
        }


        [LabelText("是否显示台词在场景上"), LabelWidth(130)]
        public bool showOnScene = false;
        [LabelText("文本 id"), ValueDropdown("GetLineKey", DropdownWidth = 300), OnValueChanged("switchContent")]
        public string lines;

        [HideLabel, TextArea, Sirenix.OdinInspector.ReadOnly]
        public string content;

        // 切换角色数据后
        private void actorChanged(ActorData data)
        {
            if (data == null)
                return;
            if (data.Icon == null)
                return;
            
            header = data.Icon;
        }

        private string[] GetPortraitIcon()
        {
            if (actor == null)
                return null;
            
            return actor.icon_portrait;
        }

        private string[] GetRoleName()
        {
            if (actor == null)
                return null;

            return actor.role_names;
        }

        private List<string> GetLineKey()
        {
            var dialog = (graph as DialogGraph);
            if (dialog == null)
                return null;

            var n = (dialog.owner as DialogNode);
            if (n == null)
                return null;

            return  n.property.GetLines();
        }
        
        private void switchContent(string lineKey)
        {
            var dialog = (graph as DialogGraph);
            if (dialog == null)
                return;
            
            var n = (dialog.owner as DialogNode);
            if (n == null)
                return;
            // switch content
            content = n.property.GetLineContent(lineKey);
        }
    }
}